import axios from "axios";
import qs from "qs";
import { PublicService } from '@/utils/public';
import configService from '@/utils/config';
const publicService: PublicService = PublicService.getInstance();
import jsonp from "jsonp";
import { useIonRouter } from '@ionic/vue';
import routerConfig from '@/utils/router';
import bus from '@/utils/bus';
// const ionRouter = useIonRouter();
// axios.defaults.baseURL = ''  //正式
axios.defaults.baseURL = '' //测试
declare let window: any
//post请求头
axios.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded;charset=UTF-8";
//允许跨域携带cookie信息
axios.defaults.withCredentials = false;
//设置超时
axios.defaults.timeout = 30000;

axios.interceptors.request.use(
  config => {
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

axios.interceptors.response.use(
  response => {
    // 
    if (response.status == 200) {
      return Promise.resolve(response);
    } else {
      return Promise.reject('error');
    }
  },
  error => {
    // publicService.closeLoading();
    return Promise.reject('error');
  }
);
export default {
  /**
   * @param {String} url 
   * @param {Object} data 
   * @returns Promise
   */
  post(url: string, data: any, showloading: boolean, headers?: string) {
    // url += '?app_token=' + configService.token;
    // data.token = configService.token;
    // console.log(url);
    data.V_CREATE_USER = data.LOGIN_NAME = configService.loginInfo.loginName;
    let loading: any;
    if (showloading) {
      // publicService.responseStatus = true;
      loading = publicService.Thsloading();
    }
    return new Promise((resolve, reject) => {
      axios({
        method: 'post',
        url,
        data: headers ? data : qs.stringify(data),
        headers: {
          'Content-Type': headers || 'application/x-www-form-urlencoded;charset=UTF-8'
        }
      })
        .then((res: any) => {
          publicService.closeLoading(loading);
          if (res.data.code * 1 === 304) {
            bus.emit('goLoginPage');
            if (res.message || res.data.message) {
              publicService.thsToast(res.message || res.data.message);
            }
            resolve('error');
          } else if (res.data.code * 1 === 200 || res.status * 1 === 200) {

            resolve(res.data);
          } else if (res.data.head && res.data.head.statusCode * 1 === 200) {
            resolve(res.data);
          } else {
            if (res.message || res.data.message) {
              publicService.thsToast(res.message || res.data.message);
            }
          }
        })
        .catch(err => {
          publicService.closeLoading(loading);
          reject('error')
        });
    })
  },

  get(url: string, data: any, showloading: boolean) {
    // url += '?app_token=' + configService.token;
    let loading: any;
    if (showloading) {
      loading = publicService.Thsloading();
    }
    if (url.indexOf('/authkey') === -1) {
      data.V_CREATE_USER = data.LOGIN_NAME = configService.loginInfo.loginName;
      data.time = new Date().getTime();
    }
    return new Promise((resolve, reject) => {
      axios({
        method: 'get',
        url,
        params: data,
      })
        .then((res: any) => {
          // console.log(res)
          publicService.closeLoading(loading);
          if (res.data.code * 1 === 304) {
            resolve('error');
            bus.emit('goLoginPage');
            if (res.message || res.data.message) {
              publicService.thsToast(res.message || res.data.message);
            }
          } else if (res.data.code * 1 === 200) {
            resolve(res.data);
          } else {
            resolve(res.data);
            if (res.message || res.data.message) {
              publicService.thsToast(res.message || res.data.message);
            }
          }
        })
        .catch(err => {
          publicService.closeLoading(loading);
          reject('error')
        });
    })
  },

  jsonp(url: string, data: any, showloading: boolean) {
    const params = [];
    for (const i in data) {
      params.push(`${i}=${data[i]}`);
    }
    url = url + '&' + params.join('&');
    return new Promise((resolve, reject) => {
      jsonp(
        url /* this.api.baseURL+url+"?aptId=CB23E3EE796B425EB2A6CC867BF79314" */,
        undefined,
        (err, data) => {
          if (err) {
            reject('error');
          } else {
            resolve(data);
          }
        }
      )
    })

  }
};